<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script>
	$(function(){
		$(":input[name=findWord]").keypress(function(event){
			if(event.keyCode == 13){
				$("#findFriendBtn").click();
				return false;
			}
		});
		$("#findFriendBtn").click(function(){
			//alert(11);
			//var findType=$(":input[name=findType]").val();
			var findWord=$(":input[name=findWord]").val();
			if(findWord==""){
				$(":input[name=findWord]").focus();
				$("#findFriendResultView").empty();
				alert("검색어를 입력하세요!");
				return;
			}
			$.ajax({
				type:"get",
				url:"${initParam.root}messenger/auth_findFriend.do",
				data:$("#findFriendForm").serialize(),
				dataType:"json",
				beforeSend:function(){
					$("#findFriendResultView").empty();
				},
				success:function(jsonData){
					if(jsonData.flag=="selfFind"){
						if(confirm("내 정보로 이동하시겠습니까?")){
							location.href="${initParam.root}mypage/auth_myPageMenuView.do";
						}
						$(":input[name=findWord]").val("");
						return;
					}
					if(jsonData.list.length==0){
						alert("검색결과가 없습니다.");
						$(":input[name=findWord]").val("");
						return;
					}
					//alert(jsonData[0].name);
					var tableData="<table cellpadding=5><thead><tr><th>ID</th><th>NAME</th><th>ADDRESS</th></tr></thead><tbody>";
					$.each(jsonData.list,function(index,value){
						//alert(value.name+" "+value.address+" "+value.id);
						tableData+="<tr>";
						tableData+="<td>"+value.id+"</td>";
						tableData+="<td>"+value.name+"</td>";
						tableData+="<td>"+value.address+"</td>";
						tableData+="<td><input type='button' value='친구추가' id='addFriendBtn'></td>";
						tableData+="</tr>";
					});
					$("#findFriendResultView").append(tableData);
					///alert($("#findFriendResultView").html());
				} // success
			}); // ajax
			$("#findFriendResultView").on("click","#addFriendBtn",function() {
				//alert($(this).parent().parent().children().eq(0).text());
				location.href="${initParam.root}messenger/auth_addFriend.do?friendId="+$(this).parent().parent().children().eq(0).text();
			}); // on
		}); // click
	});
</script>
<center>
<b>친구검색</b><br><br>
<form method="get" id="findFriendForm">
	<select name="findType">
		<option value="id">아이디</option>
		<option value="name">이름</option>
		<option value="address">주소</option>
	</select>
	<input type="text" name="findWord">
	<!-- <input type="button" value="검색" id="findFriendBtn"> -->
	<img src="${initParam.root }img/search_btn.jpg" height="25" id="findFriendBtn" class="img_button">
</form>
<br>
<br>
<br>
<div id="findFriendResultView"></div>
</center>